package com.ssww.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ssww.dao.ContactDao;
import com.ssww.entity.Contact;

/**
 * dao实现
 * 
 * @author 
 * 
 */
@SuppressWarnings("unchecked")
@Repository
public class ContactDaoImpl extends HibernateDaoSupport implements ContactDao {
	
	@Autowired
	public ContactDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
	    setSessionFactory(sessionFactory);
	}
	@Transactional
	public Contact getContactById(Integer id) {
		return this.getHibernateTemplate().get(Contact.class, id);
	}
	@Transactional
	public List<Contact> getContactList() {
		String hql ="select * from contact";
		SessionFactory sf = this.getSessionFactory();
		Session session = sf.openSession();
	    SQLQuery query = session.createSQLQuery(hql);
		query.addEntity(Contact.class);
		return (List<Contact>)query.list();
	}
	@Transactional
	public int addContact(Contact a) {
		this.getHibernateTemplate().save(a);
		return 0;
	}
	@Transactional
	public int deleteContactr(int id) {
		Contact a=this.getHibernateTemplate().get(Contact.class,id);
		this.getHibernateTemplate().delete(a);
		return 0;
	}
	@Transactional
	public int updateContact(Contact a, int id) {
		Contact c=this.getHibernateTemplate().get(Contact.class, id);
		this.getHibernateTemplate().update(c);
		return 0;
	}
	@Transactional
	public int deleteContact(int id) {
		Contact a=this.getHibernateTemplate().get(Contact.class,id);
		this.getHibernateTemplate().delete(a);
		return 0;
	}

}


